﻿using System.Linq;
using Metro.Kashikoi.Common.ExceptionHandling;
using Metro.Kashikoi.Service.Common.Entity;
using Metro.Kashikoi.ServiceContract.Item;
using ItemBase = Metro.Kashikoi.ServiceContract.Item.Item;

namespace Metro.Kashikoi.Service.Item.Entity
{
    /// <summary>
    /// 
    /// </summary>
    static class Repository
    {
        #region Extension Methods

        /// <summary>
        /// Gets the vendor.
        /// </summary>
        /// <param name="repository">The repository.</param>
        /// <param name="id">The id.</param>
        /// <returns>The vendor.</returns>
        public static Vendor GetVendor(this Repository<Vendor> repository, int id)
        {
            if (repository.Count(o => o.Id == id && o.Deleted == false) == 1)
            {
                return repository.Single(o => o.Id == id && o.Deleted == false);
            }
            else
            {
                throw new NotExistException
                {
                    Entity = "Vendor",
                    Value = id,
                };
            }
        }

        /// <summary>
        /// Gets the category.
        /// </summary>
        /// <param name="repository">The repository.</param>
        /// <param name="id">The id.</param>
        /// <returns>The Category.</returns>
        public static ColorCategory GetCategory(this Repository<Category> repository, int id)
        {
            if (repository.Count<ColorCategory>(o => o.Id == id && o.Deleted == false) == 1)
            {
                return repository.Single<ColorCategory>(o => o.Id == id && o.Deleted == false);
            }
            else
            {
                throw new NotExistException
                {
                    Entity = "Category",
                    Value = id,
                };
            }
        }

        /// <summary>
        /// Gets the item.
        /// </summary>
        /// <param name="repository">The repository.</param>
        /// <param name="id">The id.</param>
        /// <returns>
        /// The item.
        /// </returns>
        public static StockItem GetItem(this Repository<ItemBase> repository, int id)
        {
            if (repository.Count<StockItem>(o => o.Id == id && o.Deleted == false) == 1)
            {
                return repository.Single<StockItem>(o => o.Id == id && o.Deleted == false);
            }
            else
            {
                throw new NotExistException
                {
                    Entity = "Stock Item",
                    Value = id,
                };
            }
        }

        /// <summary>
        /// Gets the charge department.
        /// </summary>
        /// <param name="repository">The repository.</param>
        /// <param name="id">The id.</param>
        /// <returns>The charge department</returns>
        public static ChargeDepartment GetChargeDepartment(this Repository<ChargeDepartment> repository, int id)
        {
            if (repository.Count(o => o.Id == id && o.Deleted == false) == 1)
            {
                return repository.Single(o => o.Id == id && o.Deleted == false);
            }
            else
            {
                throw new NotExistException
                {
                    Entity = "Charge Department",
                    Value = id,
                };
            }
        }

        #endregion
    }
}